package com.ibiaoke.system.service;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.system.dao.XcxAdvertDao;
import com.ibiaoke.system.entity.XcxAdvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
public class XcxAdverServiceImpl implements  XcxAdverService{
    @Autowired
    private XcxAdvertDao xcxAdvertDao;

    @Override
    public List<XcxAdvert> listBanner(XcxAdvert xcxAdvert,Page page) {
        AssertUtils.notNull(xcxAdvert.getPosition(), "指定位置不能为空");

        List<Integer> shopIds = new ArrayList<>();

        if (xcxAdvert.getShopId() != null) {
            shopIds.add(CommonConsts.NO);
            shopIds.add(xcxAdvert.getShopId());
        } else {
            shopIds = null;
        }
        return xcxAdvertDao.listBanner(xcxAdvert,shopIds,page);
    }

    @Override
    public int saveAdvert(XcxAdvert xcxAdvert) {
        AssertUtils.notNull(xcxAdvert.getType(), "链接类型不能为空");
        switch (xcxAdvert.getType()) {
            case 1:
                AssertUtils.notBlank(xcxAdvert.getUrl(), "广告的链接地址不能为空");
                break;
            case 2:
                AssertUtils.notNull(xcxAdvert.getObjectId(), "商品Id不能为空");
                break;
            case 3:
                AssertUtils.notNull(xcxAdvert.getObjectId(), "分类Id不能为空");
                break;
            default:
                AssertUtils.notNull(xcxAdvert.getType(), "类型填写错误");
                break;
        }
        AssertUtils.notNull(xcxAdvert.getShopId(),"店铺Id不能为空");
        AssertUtils.notBlank(xcxAdvert.getImage(), "广告图片路径不能为空");
        AssertUtils.notNull(xcxAdvert.getPosition(), "广告指定位置不能为空");
        xcxAdvert.setShowFlag(1);
        xcxAdvert.setDelStatus(0);
        xcxAdvert.setCreateTime(new Date());
        xcxAdvert.setUpdateTime(new Date());
        return xcxAdvertDao.saveBanner(xcxAdvert);
    }

    @Override
    public int updateBannerById(XcxAdvert xcxAdvert) {
        AssertUtils.notNull(xcxAdvert.getId(),"广告编号不能为空");
        xcxAdvert.setUpdateTime(new Date());

        return xcxAdvertDao.updateBannerById(xcxAdvert);
    }

    @Override
    public int deleteBannerById(Integer xcxAdvertId) {
        AssertUtils.notNull(xcxAdvertId,"广告编号不能为空");
        return xcxAdvertDao.deleteBannerById(xcxAdvertId);
    }

    @Override
    public int batchDeleteXcxAdver(String xcxAdvertIds) {
        AssertUtils.notBlank(xcxAdvertIds,"广告编号不能为空");
        return xcxAdvertDao.batchDeleteXcxAdver(xcxAdvertIds);
    }
}
